package com.asm.service;

import com.asm.dto.PageResult;
import com.asm.entity.sys.CommunityTag;
import com.asm.repository.CommunityTagRepository;
import com.asm.util.SnowflakeIdWorker;
import com.asm.vo.sys.CommunityTagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Tanzujie
 */
@Service
public class CommunityTagService {
    @Resource
    private CommunityTagRepository communityTagRepository;
    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 获取小区标签列表
     *
     * @param page 分页
     * @return 标签列表
     */
    private List<CommunityTagVo> getCommunityTagVoList(Page<CommunityTag> page) {
        List<CommunityTagVo> list = new ArrayList<>();
        for (CommunityTag communityTag : page.getContent()) {
            CommunityTagVo communityTagVo = new CommunityTagVo();
            BeanUtils.copyProperties(communityTag, communityTagVo);
            list.add(communityTagVo);
        }
        return list;
    }

    /**
     * 分页查询所有小区标签
     *
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 小区标签
     */
    public PageResult<CommunityTagVo> findAllCommunityTags(Integer pageNumber, Integer pageSize) {
        Page<CommunityTag> page = communityTagRepository.findAll(PageRequest.of(pageNumber, pageSize));
        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), getCommunityTagVoList(page));
    }

    /**
     * 根据id查询小区标签
     *
     * @param id 编号
     * @return 小区标签
     */
    public CommunityTagVo findCommunityTagById(Long id) {
        CommunityTag communityTag = communityTagRepository.findById(id).get();
        CommunityTagVo communityTagVo = new CommunityTagVo();
        BeanUtils.copyProperties(communityTag, communityTagVo);
        return communityTagVo;
    }

    /**
     * 根据小区标签类型分页查询小区标签
     *
     * @param type       小区标签类型
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 小区标签
     */
    public PageResult<CommunityTagVo> findCommunityTagBytagType(String type, Integer pageNumber, Integer pageSize) {
        Pageable page = PageRequest.of(pageNumber, pageSize);
        Page<CommunityTag> communityTags = communityTagRepository.findByTagTypeAndIsDel(type, false, page);
        return new PageResult<>(pageNumber, pageSize, communityTags.getTotalPages(), getCommunityTagVoList(communityTags));
    }

    /**
     * 根据小区编号分页查询小区标签
     *
     * @param communityid 小区编号
     * @param pageNumber  当前页码
     * @param pageSize    页面大小
     * @return 小区标签
     */
    public PageResult<CommunityTagVo> findCommunityTagByCommunityId(Long communityid, Integer pageNumber, Integer pageSize) {
        Pageable page = PageRequest.of(pageNumber, pageSize);
        Page<CommunityTag> communityTags = communityTagRepository.findByCommunityIdAndIsDel(communityid, false, page);
        return new PageResult<>(pageNumber, pageSize, communityTags.getTotalPages(), getCommunityTagVoList(communityTags));
    }

    /**
     * 添加小区标签
     *
     * @param communityTagVo 小区标签对象
     * @return 操作状态
     */
    @Transactional(rollbackFor = Exception.class)
    public Long addCommunityTag(CommunityTagVo communityTagVo) {
        CommunityTag communityTag = new CommunityTag();
        BeanUtils.copyProperties(communityTagVo, communityTag);
        communityTag.setId(snowflakeIdWorker.nextId());
        communityTag.setCreateAt(new Date());
        communityTag.setCreateBy(1L);
        communityTag.setUpdateAt(new Date());
        communityTag.setUpdateBy(1L);
        communityTagRepository.save(communityTag);
        return communityTag.getId();
    }

    /**
     * 更新小区标签
     *
     * @param communityTagVo 小区标签对象
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateCommunityTag(CommunityTagVo communityTagVo) {
        CommunityTag communityTag = communityTagRepository.findById(communityTagVo.getId()).get();
        BeanUtils.copyProperties(communityTagVo, communityTag);
        communityTag.setUpdateBy(1L);
        communityTag.setUpdateAt(new Date());
        communityTagRepository.save(communityTag);
    }

    /**
     * 根据Id删除小区标签
     *
     * @param id 小区id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteCommunityTag(Long id) {
        CommunityTag communityTag = communityTagRepository.findById(id).get();
        communityTag.setIsDel(true);
        communityTag.setUpdateBy(1L);
        communityTag.setUpdateAt(new Date());
        communityTagRepository.save(communityTag);
    }

}
